<?php

namespace App\Repository\OnlineLogs;

use App\Models\OnlineLogsModel as Model;
use App\Repository\BaseRepository;

class OnlineLogsRepository extends BaseRepository
{
    public $model;
    public $log;

    public function __construct()
    {
        $this->model = new Model();
//    $this->log = new Logger('anchor');
//    $this->log->pushHandler(
//      (new RotatingFileHandler(storage_path('/logs/anchor.log'), 14, Logger::DEBUG, true, 0777))->setFormatter(new LineFormatter(null, null, true, true))
//    );
        parent::__construct();
    }

    //列表
    public function index($params, $page = 1, $limit = 10)
    {
        $where = [];
        if (isset($params['ordersno']) && !empty($params['ordersno'])) {
            $where['eq']['ordersno'] = trim($params['ordersno']);
        }
        if (isset($params['product_id']) && !empty($params['product_id'])) {
            $where['eq']['product_id'] = trim($params['product_id']);
        }
        if (isset($params['dateRange']) && !empty($params['dateRange'])) {
            $time = $params['dateRange'];
            $where['between'] = ['created_at' => [$time]];
        }
        //        先要总数
        $totalRows = 0;
        if ($page == 1) {
            $totalRows = $this->model->searchWhere($where)->count('id');
        }
        $rows = $this->model
            ->searchWhere($where)
            ->page($page, $limit)->orderByDesc('id')->get();
        return $this->returnMsg(200, '查询成功', $rows, $totalRows);
    }

    //修改
    public function edit($params = [])
    {
        return $this->returnMsg(20, '修改错误');
    }
}
